<template>
  <Teleport to="body">
    <Transition name="message-fade">
      <div
        v-if="visible"
        class="custom-message"
        :class="[`custom-message--${type}`]"
      >
        <div class="custom-message__icon">
          <component :is="iconComponent" />
        </div>
        <div class="custom-message__content">{{ message }}</div>
        <div class="custom-message__close" @click="close">×</div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import {
  CircleCheck,
  Warning,
  CircleClose,
  InfoFilled
} from '@element-plus/icons-vue'

interface Props {
  message: string
  type?: 'success' | 'warning' | 'error' | 'info'
  duration?: number
  onClose?: () => void
}

const props = withDefaults(defineProps<Props>(), {
  type: 'info',
  duration: 3000
})

const visible = ref(false)

const iconComponent = computed(() => {
  const iconMap = {
    success: CircleCheck,
    warning: Warning,
    error: CircleClose,
    info: InfoFilled
  }
  return iconMap[props.type]
})

const close = () => {
  visible.value = false
  setTimeout(() => {
    props.onClose?.()
  }, 300)
}

onMounted(() => {
  visible.value = true
  if (props.duration > 0) {
    setTimeout(() => {
      close()
    }, props.duration)
  }
})
</script>

<style scoped>
.custom-message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  background: white;
  border: 1px solid #e4e7ed;
  min-width: 300px;
  max-width: 500px;
}

.custom-message--success {
  border-color: #67c23a;
  background-color: #f0f9ff;
}

.custom-message--warning {
  border-color: #e6a23c;
  background-color: #fdf6ec;
}

.custom-message--error {
  border-color: #f56c6c;
  background-color: #fef0f0;
}

.custom-message--info {
  border-color: #409eff;
  background-color: #ecf5ff;
}

.custom-message__icon {
  margin-right: 8px;
  font-size: 16px;
}

.custom-message--success .custom-message__icon {
  color: #67c23a;
}

.custom-message--warning .custom-message__icon {
  color: #e6a23c;
}

.custom-message--error .custom-message__icon {
  color: #f56c6c;
}

.custom-message--info .custom-message__icon {
  color: #409eff;
}

.custom-message__content {
  flex: 1;
  font-size: 14px;
  color: #606266;
}

.custom-message__close {
  margin-left: 8px;
  cursor: pointer;
  font-size: 16px;
  color: #c0c4cc;
  font-weight: bold;
}

.custom-message__close:hover {
  color: #909399;
}

.message-fade-enter-active,
.message-fade-leave-active {
  transition: all 0.3s ease;
}

.message-fade-enter-from {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}

.message-fade-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}
</style>
